<!DOCTYPE html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
         
        <script type='text/javascript' src="../js/jquery.js"></script>
        <script type="text/javascript">
 /*           $(function () {
             
                window.setInterval(function () {
                 
                    $.get("/xProgram/ajax/test.do", 
                        {"timed": new Date().getTime()}, 
                        function (data) {
                            $("#logs").append("[data: " + data + " ]<br/>");
                    });
                }, 3000);
                 
            });*/
            
            $(function () {
                
                (function longPolling() {
                 
                    $.ajax({
                        url: "/xProgram/ajax/test.do?a=dddd",
                        data: {"timed": new Date().getTime()},
                        dataType: "text",
                        timeout: 5000,
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            $("#logs").append("[state: " + textStatus + ", error: " + errorThrown + " ]<br/>");
                            if (textStatus == "timeout") { // 请求超时
                                    longPolling(); // 递归调用
                                 
                                // 其他错误，如网络错误等
                                } else { 
                                    longPolling();
                                }
                            },
                        success: function (data, textStatus) {
                            $("#logs").append("[state: " + textStatus + ", data: { " + data + "} ]<br/>");
                             
                            if (textStatus == "success") { // 请求成功
                                longPolling();
                            }
                        }
                    });
                })();
                 
            });
        </script>
    </head>
     
    <body>
        <div id="logs"></div>
    </body>
</html>